<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\helpers\v1\traits\SearchDataForModel;
use app\api\model\v1\AdminLog;
use app\api\model\v1\AuthWxMenu;
use app\api\model\v1\WxMenu;
use app\BaseController;
use think\Request;

class WxMenuCon extends BaseController
{
    use SearchDataForModel;

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 10:04
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 数据源--PC端
     */
    public function getWxMenuList(){
        $limit = $this->request->param('limit',10);
        $type = $this->request->param('type','page');
        if ($type === 'no_page'){
            $res =  WxMenu::where(['status'=>1])->order('sort asc')->select();
        }else{
            $res = $this->search(new WxMenu(),[],$limit,'asc');
        }

        return $this->response($res);
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 14:24
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 菜单微信端
     */
    public function getWxMenuInfo(){
        $data  = $this->request->param();

        if (intval($data['user_id']) !== 0){
            if($this->auth->isSuperAdmin($data['user_id'])){
                $res = WxMenu::where(['status'=>WxMenu::IS_STATUS_ONE,'is_home'=>WxMenu::IS_HOME_ONE])->select()->toArray();
                foreach ($res as $key=>$v){
                    $res[$key]['wxMenu'] = $v;
                }
            }else{
                $res =AuthWxMenu::with(['wxMenu'=>function($query){
                    $query->where(['status'=>WxMenu::IS_STATUS_ONE,'is_home'=>WxMenu::IS_HOME_ONE]);
                }])->where(['group_id'=>$data['group_id']])->order('id asc')->select()->filter(function (AuthWxMenu $authWxMenu){
                    if (!empty($authWxMenu->wxMenu)){
                        return true;
                    }
                })->toArray();
            }
        }else{
            //默认显示
            $res =AuthWxMenu::with(['wxMenu'=>function($query){
                $query->where(['status'=>WxMenu::IS_STATUS_ONE,'is_home'=>WxMenu::IS_HOME_ONE]);
            }])->where(['group_id'=>2])->order('id asc')->select()->filter(function (AuthWxMenu $authWxMenu){
                if (!empty($authWxMenu->wxMenu)){
                    return true;
                }
            })->toArray();
        }

        return $this->response(array_values($res));
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 15:02
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 显示更多
     */
    public function getWxMenuInfoMore(){
        $data  = $this->request->param();
        $new_arr = [];
        if($this->auth->isSuperAdmin($data['user_id'])){
            $res = WxMenu::where(['status'=>WxMenu::IS_STATUS_ONE])->select()->toArray();
            foreach ($res as $key=>$v){
                $res[$key]['wxMenu'] = $v;
                $new_arr[$res[$key]['wxMenu']['menu_type']]['title'] = WxMenu::$menu_type_arr[$v['menu_type']];
                $new_arr[$res[$key]['wxMenu']['menu_type']]['child'][] = $v;
            }
        }else{
            $res =AuthWxMenu::with(['wxMenu'])->where(['group_id'=>$data['group_id']])->select()->toArray();
            foreach ($res as $key=>$val){
                $new_arr[$val['wxMenu']['menu_type']]['title'] = WxMenu::$menu_type_arr[$val['wxMenu']['menu_type']];
                $new_arr[$val['wxMenu']['menu_type']]['child'][] = $val['wxMenu'];
            }
        }
        return $this->response(array_values($new_arr));
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 10:02
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 保存微信菜单
     */
    public function saveMenu(){
        $id = $this->request->param('id', 0);
        $data = $this->request->param();
        $data['uid'] = $this->request->uid; //用户id
        if (intval($id) === 0) {
            WxMenu::create($data);
        } else {
            $res = WxMenu::find($id);
            if (is_null($res)) {
                return $this->response(403, '数据不存在');
            }
            $res->save($data);
        }
        AdminLog::record(intval($id) === 0 ? '新增微信菜单' : '编辑微信菜单', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 11:32
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除菜单
     */
    public function deleteWxMenu($id)
    {
        $res = WxMenu::with(['wxMenuAuth'])->find($id);
        if (is_null($res)) {
            return $this->response(403, '找不到该资源信息');
        }
        $res->together(['wxMenuAuth'])->delete();
        AdminLog::record('删除微信菜单', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/31 14:40
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 保存微信权限菜单
     */
    public function saveAuthWxMenu(){
        $data = $this->request->param();

        $res = AuthWxMenu::where(['group_id'=>$data['id']])->select();
        if(!$res->isEmpty()){
            $res->delete();
        }
        foreach ($data['rules'] as $key=>$v){
            AuthWxMenu::create([
                'group_id'=>$data['id'],
                'wx_menu_id'=>$v
            ]);
        }
        $content = [
            'group_id'=>$data['id'],
            'wx_menu_id'=>json_encode($data['rules'])
        ];
        AdminLog::record('添加微信全选菜单', $content, $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/31 15:41
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 获取微信菜单权限读取
     */
    public function getAuthWxMenuList(){
        $group_id = $this->request->param('group_id');
        $res = AuthWxMenu::field('wx_menu_id')->where(['group_id'=>$group_id])->select();
        $new_arr = [];
        foreach ($res as $key=>$v){
            $new_arr[] = $v['wx_menu_id'];
        }
        if(empty($new_arr)){
            return $this->response(403,'暂无数据');
        }else{
            return $this->response($new_arr);
        }
    }
}
